#pragma once
#include <iostream>
#include <boost/asio.hpp>
#include <queue>
const int READSIZE = 1024;
class MsgNode
{
public:
	MsgNode(const char* msg, int len) :_total_len(len), _cur_len(0) {
		_msg = new char[_total_len];
		memcpy(_msg, msg, len);
	}
	MsgNode(int len) :_total_len(len), _cur_len(0) {
		_msg = new char[_total_len];
	}
	~MsgNode() {
		delete[]_msg;
	}
	char* _msg;
	int _total_len;
	int _cur_len;
};


class Session{
public:
	Session(std::shared_ptr<boost::asio::ip::tcp::socket> sock_ptr);
	void ReadAllFromSocket();
	void ReadAllCallBack(const boost::system::error_code& ec, std::size_t bytes_transferred);
	void ReadFromSocket();
	void ReadCallBack(const boost::system::error_code& ec, std::size_t bytes_transferred);
	void Connect(boost::asio::ip::tcp::endpoint& ep);
	void WriteAllToSocket(const std::string& buff);
	void WriteAllCallBack(const boost::system::error_code ec, std::size_t bytes_transferred);
	void WriteToSocket(const std::string& buff);
	void WriteCallBack(const boost::system::error_code ec, std::size_t bytes_transferred);
	void WriteToSocketErr(const std::string& buff);
	void WriteCallBackErr(const boost::system::error_code ec, std::size_t bytes_transferred,
		std::shared_ptr<MsgNode> send_msg);

private:
	std::shared_ptr<boost::asio::ip::tcp::socket> _sock_ptr;
	bool _send_pending = false;
	std::queue<std::shared_ptr<MsgNode>> _msg_queue;
	bool _read_pending = false;
	std::shared_ptr<MsgNode> _read_msg;
};


